package com.smooth.mall.controller;

import com.github.pagehelper.PageInfo;
import com.smooth.mall.common.ApiRestResponse;
import com.smooth.mall.common.Constant;
import com.smooth.mall.exception.SmoothMallException;
import com.smooth.mall.exception.SmoothMallExceptionEnum;
import com.smooth.mall.model.pojo.Category;
import com.smooth.mall.model.pojo.User;
import com.smooth.mall.request.AddCategoryReq;
import com.smooth.mall.request.UpdateCategoryReq;
import com.smooth.mall.service.CategoryService;
import com.smooth.mall.service.UserService;

import com.smooth.mall.model.vo.CategoryVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
public class CategoryController {
    /*
    * 新增目录方法
    * 需要获取session，完成权限验证，只能管理员才能进行目录操作，需要进行校验操作
    * 目录属性参数，不使用pojo中的类，单一职责原则，pojo中的类用来存储数据表的数据，
    * 需封装一个请求类，来存储前端的目录新增请求数据，保证数据的安全性
    * */
    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    @ApiOperation("后台添加商品分类")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session, @RequestBody AddCategoryReq
            addCategoryReq){
/*        //多值空值校验，入参校验
        //设置@RequestBody注解，可以在请求的body中获取到参数
        if(addCategoryReq.getName()==null||addCategoryReq.getType()==null||addCategoryReq.getParentId()
                ==null||addCategoryReq.getOrderNum()==null){
            //由于是
            return ApiRestResponse.error(SmoothMallExceptionEnum.PARA_NOT_NULL);
        }*/
        //身份校验
        User currentUser = (User)session.getAttribute(Constant.SMOOTH_MALL_USER);
        System.out.println(currentUser);
        if(currentUser==null){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if(adminRole){
            //是管理员，进行操作
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台修改商品分类")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(HttpSession session, @Valid @RequestBody UpdateCategoryReq
            updateCategoryReq) throws SmoothMallException {
        User currentUser = (User)session.getAttribute(Constant.SMOOTH_MALL_USER);
        if(currentUser==null){
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if(adminRole){
            //是管理员，进行操作
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq,category);
            categoryService.update(category);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(SmoothMallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("后台删除目录分类")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id) throws SmoothMallException {
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录分类列表")
    @PostMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNumber,@RequestParam
            Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNumber,pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台目录分类列表")
    @PostMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer(){
        List<CategoryVO> categoryVOS = categoryService.listForCustomer(0);
        return ApiRestResponse.success(categoryVOS);
    }
}
